package com.zhuzhe.blog.category.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhuzhe.blog.base.JsonData;
import com.zhuzhe.blog.base.ResponseData;
import com.zhuzhe.blog.category.entity.Category;
import com.zhuzhe.blog.category.requestDto.CategoryDto;
import com.zhuzhe.blog.category.requestDto.CategorySerachDto;
import com.zhuzhe.blog.category.responseVo.CategoryLabelListVo;
import com.zhuzhe.blog.category.responseVo.CategoryListVo;
import com.zhuzhe.blog.category.responseVo.CategoryVo;
import com.zhuzhe.blog.category.service.ICategoryService;
import com.zhuzhe.blog.convert.Convert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * 文章分类表-前端控制器
 */
@Api(value="分类管理接口", description = "分类管理接口，提供分类的增、删、改、查")
@RestController // 所有方法都会返回json字符串进行响应
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    /**
     * 分页条件查询分类信息 ，
     * @param categoryDto
     * @return 会将对象转成json
     */
    @ApiOperation("根据分类名称与状态查询分类列表接口")
    @PostMapping("/search") // 请求路径： localhost:9537/article/category/search
    public ResponseData<IPage<CategoryVo>> search(@RequestBody CategorySerachDto categoryDto) {
        return categoryService.queryPage(categoryDto);
    }

    @ApiOperation("根据id查询分类")
    @ApiImplicitParam(name = "id",value = "类别id" ,required = true)
    @GetMapping("/{id}")
    public ResponseData<CategoryVo> searchById(@PathVariable("id") String id) {
        return JsonData.success(categoryService.getCategoryVoById(id));
    }

    @ApiOperation("修改分类")
    @PutMapping
    public ResponseData<String> updateCategoryById( @RequestBody CategoryDto categoryDto) {
        Category category = new Category();
        Convert.convertType(categoryDto,category);
        return categoryService.updateCategoryById(category);
    }


    @ApiOperation("新增分类信息")
    @PostMapping
    public ResponseData<String> save( @RequestBody CategoryDto categoryDto) {
        Category category = new Category();
        Convert.convertType(categoryDto,category);
        return categoryService.saveCategory(category);
    }


    @ApiOperation("根据id删除分类")
    @ApiImplicitParam(name = "id",value = "类别id" ,required = true)
    @DeleteMapping("/{id}")
    public ResponseData<String> deleteById(@PathVariable("id") String id) {
        return categoryService.removeCategoryById(id);
    }


    @ApiOperation("获取正常状态的分类信息")
    @GetMapping("/list")
    public ResponseData<List<CategoryListVo>> findAllNormal() {
        return JsonData.success( categoryService.findAllNormal());
    }


    @ApiOperation("获取正常状态的分类和分类下得标签信息")
    @GetMapping("/label/list")
    public ResponseData<List<CategoryLabelListVo>> findCategoryAndLabel(){
        return categoryService.findCategoryAndLabel();
    }

}
